package com.example.mybaseapp.ui.contentproviderdemo

import android.content.ContentValues
import android.net.Uri
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mybaseapp.R

class ContentProviderActivity : AppCompatActivity() {

    private lateinit var adapter: ArrayAdapter<String>
    private val itemsList = mutableListOf<String>()
    private val idsList = mutableListOf<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_content_provider)

        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, itemsList)
        val listView: ListView = findViewById(R.id.listView)
        listView.adapter = adapter

        // 添加数据
        findViewById<Button>(R.id.addButton).setOnClickListener {
            insertItem("New Item")
        }
        findViewById<Button>(R.id.delButton).setOnClickListener {
            deleteItem(idsList[0])
        }
        findViewById<Button>(R.id.searchButton).setOnClickListener {
            loadItems()
        }
        findViewById<Button>(R.id.updateButton).setOnClickListener {
            updateItem(idsList[0], "修改")
        }


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun loadItems() {
        val cursor = contentResolver.query(Uri.parse("content://com.example.mycontentprovider/items"), null, null, null, null)
        cursor?.use {
            itemsList.clear()
            idsList.clear()
            while (it.moveToNext()) {
                val name = it.getString(it.getColumnIndex("name"))
                val id = it.getInt(it.getColumnIndex("_id"))
                itemsList.add(name)
                idsList.add(id)
            }
            adapter.notifyDataSetChanged()
        }
    }

    private fun insertItem(name: String) {
        val values = ContentValues().apply {
            put("name", name)
        }
        contentResolver.insert(Uri.parse("content://com.example.mycontentprovider/items"), values)
        loadItems()
    }

    // 更新数据
    private fun updateItem(id: Int, newName: String) {
        val values = ContentValues().apply {
            put("name", newName)
        }
        val uri = Uri.parse("content://com.example.mycontentprovider/items/$id")
        contentResolver.update(uri, values, null, null)
        loadItems()
    }

    // 删除数据
    private fun deleteItem(id: Int) {
        val uri = Uri.parse("content://com.example.mycontentprovider/items/$id")
        contentResolver.delete(uri, null, null)
        loadItems()
    }
}